
public class MessagePacket
{
	//CONSTANTS
	int messageSizeInBytes = 4;
	int messageTypeInBytes = 1;
	
	//VARIABLES
	int messageLength;
	int messageType;
	byte[] payload;
	boolean hasPayload;
	
	public MessagePacket(int size,int type, byte[] data)
	{
		this.messageLength = size;
		this.messageType = type;
		this.payload = data;
		this.hasPayload = true;
	}
	
	public MessagePacket(int type)
	{
		this.messageLength = 0;
		this.messageType = type;
		this.hasPayload = false;
	}
	
	public MessagePacket(int type, int index) //have or request message
	{
		this.messageLength = 4;
		this.messageType = type;
		this.payload = this.intToBytes(index, 4);
		this.hasPayload = true;
	}
	
	public MessagePacket(int size, int type, int index, byte[] file)
	{
		this.messageLength = size;
		this.messageType = type;
		byte[] byteIndex = intToBytes(index, 4);
		this.payload = new byte[4+file.length];
		System.arraycopy(byteIndex, 0, this.payload, 0, 4);
		System.arraycopy(file, 0, this.payload, 4, file.length);
		this.hasPayload = true;
	}
	
	public byte[] messageSizeToBytes()
	{
		//Convert the size from integer to byte[messageSizeInBytes]
		String strMessageLength = Integer.toString(this.messageLength);
		byte[] messageLengthBytes = strMessageLength.getBytes();
		int length = strMessageLength.length();
		
		//In case character byte[] isnt large enough set the max amount
		if(length > messageSizeInBytes)
			length = messageSizeInBytes;
		
		byte[] toReturn = new byte[messageSizeInBytes];
		
		for(int i=0; i<length; i++)
		{
			toReturn[i] = messageLengthBytes[i];
		}
		for(int i=length; i<messageSizeInBytes;i++)
		{
			toReturn[i] = 0;
		}
		return toReturn;	
	}

	public byte[] intToBytes(int value, int arraySize)
	{
		String valueAsString = Integer.toString(value);
		byte[] valueAsBytes = valueAsString.getBytes();
		int length = valueAsString.length();
		
		//In case character byte[] isnt large enough set the max amount
		if(length > arraySize)
		{
			System.out.println("Message Packet Error: " + value + "cannot be expressed in byte[" + arraySize +"]");
			length = arraySize;
		}
		
		byte[] toReturn = new byte[arraySize];
		int difference = arraySize-length;
		for(int i=0; i < difference; i++)
			toReturn[i] = 0;
		for(int i=difference; i<arraySize; i++)
		{
			toReturn[i] = valueAsBytes[i-difference];
		}
		return toReturn;	
	}
	
	public byte[] lengthToBytes(int length, int messageSize)
	{		
		return new byte[] {
                (byte)(length >>> 24),
                (byte)(length >>> 16),
                (byte)(length >>> 8),
                (byte) length
                };
	}
	
	public byte[] getBytesToSend()
	{
		byte[] toReturn;
		byte[] length = lengthToBytes(this.messageLength, this.messageSizeInBytes);
		byte[] type = intToBytes(this.messageType, this.messageTypeInBytes);	
		if(this.hasPayload)
		{
			//add message payload
			byte[] message = this.payload;
			
			//Calculate total size
			toReturn = new byte[length.length + type.length + message.length];
			System.arraycopy(length, 0, toReturn, 0, length.length);
			System.arraycopy(type, 0, toReturn, length.length, type.length);
			System.arraycopy(message, 0, toReturn, length.length+type.length, message.length);
		}
		else
		{
			//Calculate total size
			toReturn = new byte[length.length + type.length];
			System.arraycopy(length, 0, toReturn, 0, length.length);
			System.arraycopy(type, 0, toReturn, length.length, type.length);
		}
		return toReturn;
	}


}
